package ca.ubc.icapture.genapha.actions;

import ca.ubc.icapture.genapha.forms.AnalysisFilesForm;
import ca.ubc.icapture.genapha.forms.FbatStratifyForm;
import icapture.SQLMgr;
import icapture.beans.DB.Phenotypes;
import icapture.genapha.GenotypeToolsManager;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: btripp
 * Date: Sep 13, 2006
 * Time: 9:38:59 AM
 * To change this template use File | Settings | File Templates.
 */
public class FbatStratifyAction extends AnalysisFilesAction
{

    @Override
    protected void processRequest(AnalysisFilesForm form)
    {
        final FbatStratifyForm stratForm = (FbatStratifyForm) form;
        final Integer phenotypeid = new Integer(stratForm.getSelectedPhenotype());
        GenotypeToolsManager gtm = new GenotypeToolsManager();

        // Add name to the result
        result.setName("Ethnicity Stratification");
        SQLMgr.updateResult(result);

        Phenotypes phen = SQLMgr.getPhenotype(phenotypeid);
        if (phen.getType() == Phenotypes.BINARY)
        {
            gtm.writeCombinedfbatBinaryVariableInputFileFromDB(phenotypeid, stratForm.getSelectedCohort(),
                    stratForm.isRemoveMzTwins(), stratForm.isRemoveDuplicateSubjects(),
                    stratForm.getSelectedGenes(), stratForm.getEmail(), result, gtm.FBAT_FORMAT, false,
                    stratForm.getSelectedEthnicity(), null, false, false);
        }
        else
        {
            gtm.writefbatContinousVariableInputFileFromDB(stratForm.getEmail(),
                    Integer.valueOf(stratForm.getSelectedCohort()[0]), phenotypeid,
                    stratForm.isRemoveMzTwins(), stratForm.isRemoveDuplicateSubjects(), result,
                    new ArrayList(Arrays.asList(stratForm.getSelectedEthnicity())));

        }
    }
}